package org.shangda.wechat.pet.util;


import com.power.common.util.DateTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: Jason.zhu
 * @Create: 2019/08/12 11:17
 */
@Component
@Slf4j
public class RedisCacheUtil {
    private long default_time_out = 30 * 60 * 1000;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * redis 锁定
     * @param key
     * @param millisecond
     * @return
     */
    public synchronized boolean lock(String key, long millisecond) {
        if (redisTemplate.opsForValue().setIfAbsent(key, DateTimeUtil.long2Str(System.currentTimeMillis(),DateTimeUtil.DATE_FORMAT_SECOND),
                millisecond,TimeUnit.MILLISECONDS)) {
            log.info("【RedisLock】成功加锁 setIfAbsent >>>>>>>>>>>>>>>>>>>>  threadId= {} key = {} aoldValue = {} time= {}", Thread.currentThread().getId(),key, System.currentTimeMillis());
            return true;
        }
        return false;
    }

    /**
     * redis 解锁
     * @param key
     */
    public synchronized void unlock(String key) {
        try {
            redisTemplate.opsForValue().getOperations().delete(key);
            log.info("【RedisLock】unlock >>>>>>>>>>>>>>>>>>>>  threadId= {} stock = {} time= {}", Thread.currentThread().getId(), 0, System.currentTimeMillis());
            return;
        } catch (Exception e) {
            log.error("【redis分布式做】解锁", e);
        }
    }

    /**
     * 刷新redis缓存中的值
     * @param key 缓存key
     * @param value 缓存中新的值
     * @param expiresMillis long 缓存中新的值的过期时间,过期时间为当前时间点与过期时间点之间的毫秒数（毫秒）
     */
    public synchronized void refreshValInRedis(String key, Object value, long expiresMillis) {
        if(null != get(key)){
            redisTemplate.delete(key);
        }
        redisTemplate.opsForValue().set(key, value, expiresMillis, TimeUnit.MILLISECONDS);
    }

    /**
     * 获取redis中的字符串值
     * @param key
     * @return
     */
    public synchronized String get(String key) {
        if(null != redisTemplate.opsForValue().get(key)){
            return redisTemplate.opsForValue().get(key).toString();
        }else {
            return null;
        }
    }

    /**
     * 获取redis中的object值
     * @param key
     * @return
     */
    public synchronized Object getObj(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 设置缓存，默认超时30分钟
     * @param key
     * @param value
     */
    public synchronized void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value, default_time_out, TimeUnit.MILLISECONDS);
    }

    /**
     * 删除缓存
     * @param key
     */
    public synchronized void remove(String key) {
        redisTemplate.delete(key);
    }

}
